package com.sichen.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sichen.common.utils.PageUtils;
import com.sichen.common.utils.Query;
import com.sichen.common.utils.R;
import com.sichen.product.dao.AttrAttrgroupRelationDao;
import com.sichen.product.dao.AttrDao;
import com.sichen.product.dao.AttrGroupDao;
import com.sichen.product.entity.AttrAttrgroupRelationEntity;
import com.sichen.product.entity.AttrEntity;
import com.sichen.product.entity.AttrGroupEntity;
import com.sichen.product.service.AttrGroupService;
import com.sichen.product.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrAttrgroupRelationDao relationDao;

    @Resource
    private AttrDao attrDao;

    @Resource
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils(page);
    }

    /**
     * 根据传递过来的分类id , 查询当前分类下的所有属性分组
     *
     * @param params    分页条件
     * @param catelogId 分类的id
     * @return 分页后的当前属性分组的数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        IPage<AttrGroupEntity> page;
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (catelogId != 0) {
            //有分类的id , 就需要在指定的分类中查询商品的分组信息
            wrapper.eq("catelog_id", catelogId);
        }
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> obj.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);

    }


    /**
     * 删除属性与属性分组的关联关系
     *
     * @param vos 传递的(属性&属性分组)的关联信息
     * @return 删除是否成功
     */
    @Override
    public R deleteRelation(AttrGroupRelationVo[] vos) {
        //1.收集所有的传递过来的 (属性&属性分组) 数据
        List<AttrAttrgroupRelationEntity> collect = Arrays.stream(vos).map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        //2.自定义删除SQL语句 , 进行批量删除
        relationDao.deleteBatchRelation(collect);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId 传递的分类id
     * @return 返回封装的数据
     */
    @Override
    public List<AttrGroupRespVo> getAttrGroupWithAttrByCatelogId(Long catelogId) {
        //1.根据分类Id查询 , 在属性分组表中查询绑定的所有的属性分组
        List<AttrGroupEntity> entityList = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId)
        );
        List<AttrGroupRespVo> collect = entityList.stream().map((items) -> {
            AttrGroupRespVo attrGroupRespVo = new AttrGroupRespVo();
            BeanUtils.copyProperties(items, attrGroupRespVo);
            return attrGroupRespVo;
        }).collect(Collectors.toList());
        //2.获取指定属性分组中的所有属性的集合
        List<AttrGroupRespVo> attrGroupRespVos = collect.stream().peek((item) -> {
            //2.获取属性分组的id
            Long attrGroupId = item.getAttrGroupId();
            List<AttrAttrgroupRelationEntity> groupIds = relationDao.selectList(
                    new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
            //3.根据属性分组id , 获取绑定的属性的id数组
            List<Long> attrIds = groupIds.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            //4.封装对应属性分组的属性的集合
            List<AttrVo> attrVoList = attrIds.stream().map((attrid) -> {
                AttrEntity attrEntity = attrDao.selectById(attrid);
                AttrVo attrVo = new AttrVo();
                BeanUtils.copyProperties(attrEntity, attrVo);
                return attrVo;
            }).collect(Collectors.toList());
            item.setAttrs(attrVoList);
        }).collect(Collectors.toList());
        return attrGroupRespVos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 1.查询当前spu对应的所有属性分组信息以及当前分组下的所有属性对应的值
        // 1.1.
        AttrGroupDao baseMapper = this.baseMapper;
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
    }

}